* Balance tests and pairwise normalized difference across NGO and non-NGO villages

// Load data
use "${output}panel_r0_r1_r2.dta", clear

// Variables to be includes in summary statistics table
global sumstats	household_size ///
				children_under_five ///
				age_household_head ///
				female_household_head ///
				education_household_head ///
				education_primary_cook ///
				monthly_expenditure ///
				below_pov_line ///
				traditional_stove_own ///
				improved_stove_own ///
				total_minutes_traditional_stove /// 
				improved_stove_use ///
				used_traditional_fuel ///
				clean_fuel_daily ///
				trad_fuel_collection_time ///
				heard_of_clean_stoves ///
				heard_of_clean_fuels ///
				smoke_unsafe ///
				at_least_one_coughcold
				
// Summary statistics restrictions
keep if surveyround == 0 // Balance at baseline only
keep if sample_household == 1 // Only for households in analytical sample (found in all three survey rounds)

// Balance table
preserve
	
	// Generate balance table in memory
	iebaltab $sumstats, grpvar(chirag_strata) normdiff stdev starsnoadd ///
		order(1 0) format(%9.2f) tblnonote ///
		rowlabels("household_size Household size @ children_under_five Number of children under five @ age_household_head Age of household head (years) @ female_household_head \(\mathbbm{1}\left( \text{Female-headed household} \right)\) @ education_household_head Education level of household head (years) @ education_primary_cook Education level of primary cook (years) @ monthly_expenditure Estimated household expenditure (INR per month) @ below_pov_line \(\mathbbm{1}\left( \text{Below poverty line} \right)\) @ traditional_stove_own \(\mathbbm{1}\left( \text{Owns traditional stove} \right)\) @ improved_stove_own \(\mathbbm{1}\left( \text{Owns improved stove} \right)\) @ total_minutes_traditional_stove Traditional-stove use (minutes per day) @ improved_stove_use \(\mathbbm{1}\left( \text{Used an improved stove in past week} \right)\) @ used_traditional_fuel \(\mathbbm{1}\left( \text{Uses traditional fuels} \right)\) @ clean_fuel_daily \(\mathbbm{1}\left( \text{Uses a clean fuel daily} \right)\) @ trad_fuel_collection_time Traditional-fuel collection (minutes per day) @ heard_of_clean_stoves \(\mathbbm{1}\left( \text{Heard of stoves that produce less smoke} \right)\) @ heard_of_clean_fuels \(\mathbbm{1}\left( \text{Heard of fuels that produce less smoke} \right)\) @ smoke_unsafe \(\mathbbm{1}\left( \text{Thinks cookstove emissions are unsafe} \right)\) @ at_least_one_coughcold \(\mathbbm{1}\left( \text{At least one case of cough/cold in past week} \right)\)") ///
		grplabels("1 NGO villages @ 0 Non-NGO villages") ///
		fixedeffect(districtcode) ///
		browse

	tempfile balance_table
	save `balance_table'

restore

// Multiple hypothesis test adjustment for balance table
tab districtcode, gen(districtcodedummy)
wyoung $sumstats, cmd(reg OUTCOMEVAR chirag_strata districtcodedummy2, vce(cluster uniquegrp)) ///
	familyp(chirag_strata) ///
	cluster(uniquegrp) ///
	bootstraps(5000) ///
	seed(20201002)
matrix list r(table)
matselrc r(table) pwyoung, c(4) // save adjusted p-values

// Add adjusted p-values to balance table
use `balance_table', clear // load balance table
gen variable_number = _n/2 - 1
replace variable_number = . if variable_number < 1
replace variable_number = . if mod(variable_number,1) > 0
levelsof variable_number, local(varnum)
gen wyoung_pvalue = .
foreach i of local varnum {
	replace wyoung_pvalue = pwyoung[`i',1] if variable_number == `i'
}
drop variable_number

// Add stars to normalized difference based on adjusted p-value
gen stars = "*" if wyoung_pvalue < 0.10 & wyoung_pvalue >= 0.05 & !mi(wyoung_pvalue)
replace stars = "**" if wyoung_pvalue < 0.05 & wyoung_pvalue >= 0.01 & !mi(wyoung_pvalue)
replace stars = "***" if wyoung_pvalue < 0.01 & !mi(wyoung_pvalue)
replace v6 = v6 + stars
drop stars wyoung_pvalue

****

// Replace with minus signs for LaTeX
foreach var of varlist v6 v7 {
	replace `var' = subinstr(`var', "-", "\(-\)",.) if _n >= 3
}

// Save balance table
save "${output}table_overview_of_sample_households.dta", replace

// Format LaTeX table
drop if _n < 4 // drop table headers
texsave using "${results}table_descriptive_statistics.tex", ///
	frag replace nonames nofix
